package com.xwaiy.xwaiyrpc.fault.retry;

import com.github.rholder.retry.*;
import com.xwaiy.xwaiyrpc.model.RpcResponse;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 复合延迟策略（先固定后指数）
 * @Author Xwaiy
 * @Date 2025/9/3 15:49
 **/
public class CompositeDelayRetryStrategy implements RetryStrategy {

    @Override
    public RpcResponse doRetry(Callable<RpcResponse> callable) throws Exception {
        Retryer<RpcResponse> retryer = RetryerBuilder.<RpcResponse>newBuilder()
                .retryIfException()
                // 复合策略：前2次固定1秒，之后指数增长
                .withWaitStrategy(WaitStrategies.join(
                        WaitStrategies.fixedWait(1000, TimeUnit.MILLISECONDS),
                        WaitStrategies.exponentialWait(2000, 30000, TimeUnit.MILLISECONDS)
                ))
                .withStopStrategy(StopStrategies.stopAfterAttempt(5))
                .build();

        return retryer.call(callable);
    }
}
